package guoanmaker.provider.business.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import guoanmaker.provider.business.model.ProvideInvoice;
import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.OrderInvoiceAddress;
import guoanmaker.provider.business.service.ProvideInvoiceService;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import redis.clients.jedis.Jedis;

@RestController
@RequestMapping(ApiController.INVOICE_URL)
public class ProvideInvoiceController {
	
	@Resource
	private ProvideInvoiceService provideInvoiceService;
	
	@Resource
	private AjaxJson ajaxJson;
	
	@Autowired  
	private  HttpServletRequest request;
	
	/**
	 * 
	* @Description: 保存服务商发票 
	* @param banAccountkName 开户行名称
	* @param bankAccountNumber 开户行账号 
	* @param companyName 公司名称
	* @param taxpayerIdentifier 纳税人识别号
	* @param companyAddress 公司注册地址
	* @param contactNumber 联系人电话
	* @param businessLicenseFile营业执照副本扫描图片
	* @param taxpayerCertificateFile一般纳税人资格证明图片   
	* @param @param businessid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 上午10:37:06     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "保存服务商发票", notes = "保存服务商发票")
	@RequestMapping(value = "/createProvideInvoice", method = RequestMethod.POST)
	public AjaxJson createProvideInvoice(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, @RequestParam(required = false)MultipartFile businessLicenseFile,
			@RequestParam(required = false)MultipartFile taxpayerCertificateFile,String businessid) {
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.createProvideInvoice(banAccountkName, bankAccountNumber, companyName, taxpayerIdentifier, companyAddress, contactNumber, businessLicenseFile, taxpayerCertificateFile, businessid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "保存服务商发票", notes = "保存服务商发票")
	@RequestMapping(value = "/createProvideInvoiceForPC", method = RequestMethod.POST)
	public AjaxJson createProvideInvoiceForPC(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, String businessLicenseFile,
			String taxpayerCertificateFile,String businessid) {
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.createProvideInvoiceForPC(banAccountkName, bankAccountNumber, companyName, taxpayerIdentifier, companyAddress, contactNumber, businessLicenseFile, taxpayerCertificateFile, businessid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 修改服务商发票 
	* @param banAccountkName开户行名称
	* @param bankAccountNumber开户行账号
	* @param companyName公司名称
	* @param taxpayerIdentifier纳税人识别号
	* @param companyAddress公司注册地址
	* @param contactNumber联系人电话
	* @param businessLicenseFile营业执照副本扫描图片
	* @param taxpayerCertificateFile一般纳税人资格证明图片
	* @param id 
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午2:03:49     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "修改服务商发票", notes = "修改服务商发票")
	@RequestMapping(value = "/updateProvideInvoice", method = RequestMethod.POST)
	public AjaxJson updateProvideInvoice(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, @RequestParam(required = false)MultipartFile businessLicenseFile,
			@RequestParam(required = false)MultipartFile taxpayerCertificateFile, String id) {
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.updateProvideInvoice(banAccountkName, bankAccountNumber, companyName, taxpayerIdentifier, companyAddress, contactNumber, businessLicenseFile, taxpayerCertificateFile, id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	@ApiOperation(value = "修改服务商发票", notes = "修改服务商发票")
	@RequestMapping(value = "/updateProvideInvoiceForPC", method = RequestMethod.POST)
	public AjaxJson updateProvideInvoiceForPC(String banAccountkName, String bankAccountNumber, String companyName,
			String taxpayerIdentifier, String companyAddress, String contactNumber, String businessLicenseFile,
			String taxpayerCertificateFile, String id) {
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.updateProvideInvoiceForPC(banAccountkName, bankAccountNumber, companyName, taxpayerIdentifier, companyAddress, contactNumber, businessLicenseFile, taxpayerCertificateFile, id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 根据服务商id查询发票信息以及账户信息 
	* @param businessid 服务商id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:01:01     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "根据服务商id查询发票信息以及账户信息", notes = "根据服务商id查询发票信息以及账户信息")
	@RequestMapping(value = "/findInvoiceInfos", method = RequestMethod.POST)
	public AjaxJson findInvoiceInfos(String businessid){
		Map map=new HashMap<>();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(map);
		}else{
			try {
				map=provideInvoiceService.findInvoiceInfos(businessid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(map);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(map);
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 服务商提交开发票申请 
	* @param invoiceid 发票基础信息id
	* @param invoiceType 发票类型  0为增值税普通发票    1为增值税专用发票
	* @param amountOfMoney 开票金额
	* @param mailingAddress 邮寄地址
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:30:30     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "服务商提交开发票申请", notes = "服务商提交开发票申请")
	@RequestMapping(value = "/createProvideInvoiceInfo", method = RequestMethod.POST)
	public AjaxJson createProvideInvoiceInfo(String invoiceid,String invoiceType,Double amountOfMoney,String mailingAddress){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.createProvideInvoiceInfo(invoiceid, invoiceType, amountOfMoney, mailingAddress);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description: 确定给服务商开发票 
	* @param id 已提交的开发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年7月28日 下午5:46:36     
	* @return AjaxJson    
	* @throws 
	 */
	/*
	@ApiOperation(value = "确定给服务商开发票", notes = "确定给服务商开发票")
	@RequestMapping(value = "/confirmInvoicing", method = RequestMethod.POST)
	public AjaxJson confirmInvoicing(String id){
		ReturnResult returnResult=new ReturnResult();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(returnResult);
		}else{
			try {
				returnResult=provideInvoiceService.confirmInvoicing(id);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(returnResult);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(returnResult);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	*/
	/**
	 * 
	* @Description: 根据id查询服务商发票基础信息 
	* @param @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月28日 下午4:55:21     
	* @return ProvideInvoice    
	* @throws 
	 */
	@ApiOperation(value = "根据id查询服务商发票基础信息 ", notes = "根据id查询服务商发票基础信息 ")
	@RequestMapping(value = "/findProvideInvoice", method = RequestMethod.POST)
	public AjaxJson findProvideInvoice(String businessid){
		ProvideInvoice invoice=new ProvideInvoice();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(invoice);
		}else{
			try {
				invoice=provideInvoiceService.findProvideInvoice(businessid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(invoice);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(invoice);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}
	
	/**
	 * 
	* @Description:查询订单发票详细信息
	* @param @param orderid
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月30日 上午11:40:01     
	* @return AjaxJson    
	* @throws 
	 */
	@ApiOperation(value = "查询订单发票详细信息", notes = "查询订单发票详细信息")
	@RequestMapping(value = "/getOrderInvoice", method = RequestMethod.POST)
	public AjaxJson getOrderInvoice(String orderid){
		OrderInvoiceAddress invoice=new OrderInvoiceAddress();
		String sessionid=request.getSession().getId();
		String sessionid1=request.getHeader("Authorization");
		if(sessionid1!=null){
			sessionid=sessionid1.substring(7,sessionid1.length());
		}
		Jedis jedis=new Jedis("123.56.48.63",6379); 
		jedis.auth("218007");
		String value=jedis.get(sessionid);
		jedis.disconnect();
		if(value==null||!value.equals("1")){
			ajaxJson.setSuccess(false).setStatus(99).setMsg("请先登录").setData(invoice);
		}else{
			try {
				invoice=provideInvoiceService.getOrderInvoice(orderid);
				ajaxJson.setSuccess(true).setStatus(200).setMsg("操作成功").setData(invoice);
			} catch (Exception e) {
				ajaxJson.setSuccess(false).setStatus(500).setMsg("操作失败").setData(invoice);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}

}
